﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Net.Mail;

public partial class Registration : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadForm();
        }
    }

    private void LoadForm()
    {
        //  user form.
        lblMessage.Text = "";
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtEmail.Text = "";
        txtCompany.Text = "";
        txtAddress.Text = "";
        txtCity.Text = "";
        lstState.ClearSelection();
        txtZip.Text = "";
        txtPhoneNumber.Text = "";
        txtPassword.Text = "";
        txtConfirmPassword.Text = "";
        lstQuestions.ClearSelection();
        txtAnswer.Text = "";

        pnlRegistration.Visible = true;
        pnlConfirmation.Visible = false;
    }

    

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            MembershipCreateStatus createStatus;
            try
            {
                Membership.CreateUser(txtEmail.Text, txtPassword.Text, txtEmail.Text, lstQuestions.SelectedValue,
                                      txtAnswer.Text, true, out createStatus);

            }
            catch (System.Exception ex)
            {
                lblMessage.Text = ex.Message;
                return;
            }

            if (createStatus != MembershipCreateStatus.Success)
            {
                switch (createStatus)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                        lblMessage.Text = "Email address already exists.";
                        break;
                    case MembershipCreateStatus.DuplicateUserName:
                        lblMessage.Text = "User name already exists.";
                        break;
                    case MembershipCreateStatus.InvalidAnswer:
                        lblMessage.Text = "Invalid security answer.";
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                        lblMessage.Text = "Invalid email address.";
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        lblMessage.Text = "Invalid password. Password must be greater than 6 characters.";
                        break;
                    case MembershipCreateStatus.InvalidUserName:
                        lblMessage.Text = "Invalid user name.";
                        break;
                    default:
                        lblMessage.Text = "Unknown error has occured.";
                        break;
                }
                return;
            }

            // Get user's profile.
            ProfileCommon profileCommon;
            profileCommon = Profile.GetProfile(txtEmail.Text);

            profileCommon.FirstName = txtFirstName.Text;
            profileCommon.LastName = txtLastName.Text;
            profileCommon.DisplayName = txtFirstName.Text + " " + txtLastName.Text;
            profileCommon.Company = txtCompany.Text;
            profileCommon.TaxId = txtTaxId.Text;
            profileCommon.StreetAddress = txtAddress.Text;
            profileCommon.City = txtCity.Text;
            profileCommon.State = lstState.SelectedValue;
            profileCommon.Zip = txtZip.Text;
            profileCommon.Phone = txtPhoneNumber.Text;

            try
            {
                profileCommon.Save();
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = ex.Message;
                return;
            }

            try
            {
                string[] userNames = { txtEmail.Text };
                Roles.AddUsersToRole(userNames, "Customer");
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = ex.Message;
                return;
            }

            pnlRegistration.Visible = false;
            pnlConfirmation.Visible = true;
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx", true);
    }
}
